#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include <X11/XKBlib.h>

Display *dis;
Window win;
XEvent report;

int main (void)  {
    dis = XOpenDisplay (":0");
    //XAutoRepeatOn(dis);
    //XAutoRepeatOff(dis);
    win = XCreateSimpleWindow (dis, RootWindow (dis, 0), 1, 1, 500, 500,
          0, BlackPixel (dis, 0), BlackPixel (dis, 0));
    XSelectInput (dis, win, KeyPressMask | KeyReleaseMask);
    XMapWindow (dis, win);
    XFlush (dis);
    KeySym keysym;
    char *keystr = NULL;
    while (1) {
        XNextEvent (dis, &report);
        
        keysym = XkbKeycodeToKeysym(dis, report.xkey.keycode, 
                                    0, report.xkey.state & ShiftMask ? 1 : 0);
        keystr = XKeysymToString(keysym);
        switch (report.type) {  
        case KeyPress:
            printf("Key %s pressed!\n", keystr);
            break;
        case KeyRelease:
            printf("Key %s released!\n", keystr);
            break;
        default:
            break;
        }  
    }

    return (0);
}
